package com.ruoyi.service.impl;

import com.ruoyi.constance.NumConstance;
import com.ruoyi.domain.News;
import com.ruoyi.domain.NewsWithType;
import com.ruoyi.domain.RangSearchObject;
import com.ruoyi.domain.common.PageResult;
import com.ruoyi.domain.entity.NewsOrigin;
import com.ruoyi.mapper.NewsMapper;
import com.ruoyi.mapper.NewsOriginMapper;
import com.ruoyi.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * 新闻Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-04
 */
@Service
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Resource
    private NewsOriginMapper newsOriginMapper;

    /**
     * 查询新闻
     *
     * @param id 新闻主键
     * @return 新闻
     */
    @Override
    public NewsWithType selectNewsById(Integer id) {
        return newsMapper.selectNewsById(id);
    }

    /**
     * 查询新闻列表
     *
     * @param news 新闻
     * @return 新闻
     */
    @Override
    public List<NewsWithType> selectNewsList(News news) {
        return newsMapper.selectNewsList(news);
    }

    /**
     * 新增新闻
     *
     * @param news 新闻
     * @return 结果
     */
    @Override
    public int insertNews(News news) {
        return newsMapper.insertNews(news);
    }

    /**
     * 修改新闻
     *
     * @param news 新闻
     * @return 结果
     */
    @Override
    public int updateNews(News news) {
        return newsMapper.updateNews(news);
    }

    /**
     * 批量删除新闻
     *
     * @param ids 需要删除的新闻主键
     * @return 结果
     */
    @Override
    public int deleteNewsByIds(Integer[] ids) {
        return newsMapper.deleteNewsByIds(ids);
    }

    /**
     * 删除新闻信息
     *
     * @param id 新闻主键
     * @return 结果
     */
    @Override
    public int deleteNewsById(Integer id) {
        return newsMapper.deleteNewsById(id);
    }

    /**
     * 查询最新新闻
     *
     * @return 新闻集合
     */
    @Override
    public List<NewsWithType> selectLastNews() {
        return newsMapper.selectLastNews();
    }

    /**
     * 按条件查询一定时间段查询新闻
     *
     * @return 新闻集合
     */
    public PageResult<NewsWithType> newsRangSearch(@RequestParam RangSearchObject searchObject) {
        // 给分页参数设置默认值
        if (searchObject.getPageNo() == null || searchObject.getPageNo() <= 0) {
            searchObject.setPageNo(1);
        }
        if (searchObject.getPageSize() == null || searchObject.getPageSize() <= 0) {
            searchObject.setPageSize(NumConstance.DEFAULT_PAGE_SIZE);
        }
        searchObject.setBegin((searchObject.getPageNo() - 1) * searchObject.getPageSize());
        System.out.println("searchObject = " + searchObject);
        List<NewsWithType> records = newsMapper.newsRangSearch(searchObject);
        int total = newsMapper.getRangSearchCount(searchObject);
        return new PageResult<>(total, records);
    }

    @Override
    public List<NewsOrigin> getNewsOriginList() {
        return newsOriginMapper.selectList(null);
    }

    /**
     * 查询对应股票ID相关的最新三条新闻
     *
     * @param stockId
     * @return 新闻集合
     */
    public List<News> listThreeNews(Integer stockId) {
        return newsMapper.listThreeNews(stockId);
    }

    @Override
    public PageResult<NewsWithType> listWatchlistNews(Integer userId, Integer pageNo, Integer pageSize) {
        List<NewsWithType> newsList = newsMapper.selectWatchlistNewsByUserId(userId, (pageNo - 1) * pageSize, pageSize);
        int total = newsMapper.countWatchlistNewsByUserId(userId);
        return new PageResult<>(total, newsList);
    }

    @Override
    public PageResult<NewsWithType> searchNewsByKeyword(String keyword, Integer pageNo, Integer pageSize) {

        List<NewsWithType> records = newsMapper.selectNewsByKeyword(keyword, ((pageNo - 1) * pageSize), pageSize);
        int total = newsMapper.getNewsCountByKeyword(keyword);

        return new PageResult<>(total, records);
    }

    @Override
    public PageResult<NewsWithType> conditionsSearch(RangSearchObject searchObject) {
        // 给分页参数设置默认值
        if (searchObject.getPageNo() == null || searchObject.getPageNo() <= 0) {
            searchObject.setPageNo(1);
        }
        if (searchObject.getPageSize() == null || searchObject.getPageSize() <= 0) {
            searchObject.setPageSize(NumConstance.DEFAULT_PAGE_SIZE);
        }
        searchObject.setBegin((searchObject.getPageNo() - 1) * searchObject.getPageSize());

        // 调用Mapper层方法进行查询
        List<NewsWithType> records = newsMapper.conditionsSearch(searchObject);
        int total = newsMapper.getConditionsSearchCount(searchObject);

        return new PageResult<>(total, records);
    }

}
